//
//  AddAddressStore.m
//  NewPro
//
//  Created by RYX on 2018/10/20.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "AddAddressStore.h"
#import "AddAddressParametersModel.h"
#import "AddressModel.h"

@implementation AddAddressStore

#pragma mark - 获取所有地址
- (void)getAllAddressWithSuccess:(void(^)(NSArray *listArr))success Failure:(void(^)(NSError *error))failure{
    
    NSString *url = [NSString stringWithFormat:@"%@/api/address/findAddressBuCustomId",IP];
    NSDictionary *dic = @{
                          @"customId":[UserDefaultsManager sharedInstance].user_id
                          };
    
    [HttpTool postUrlWithString:url parameters:dic success:^(id responseObject) {
        
        NSError *error = [HttpTool inspectError:responseObject];
        if (error == nil) {
            
            NSArray *listArr = [AddressModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"items"]];
            
            NSArray *arr = [[listArr reverseObjectEnumerator] allObjects];
            
            success(arr);
        }else{
            
            failure(error);
        }
        
    } failure:^(NSError *error) {
       
        failure(error);
    }];
    
    
}

#pragma mark - 添加地址
- (void)addAdresstWithParameters:(AddAddressParametersModel *)parametersModel WithSuccess:(void (^)(void))success failure:(void (^)(NSError * error))failure{
    
    NSString *url = [NSString stringWithFormat:@"%@/api/address/addAddress",IP];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:parametersModel.customId forKey:@"customId"];
    [dic setObject:parametersModel.name forKey:@"name"];
    [dic setObject:parametersModel.phone forKey:@"phone"];
    [dic setObject:parametersModel.region forKey:@"region"];
    [dic setObject:parametersModel.address forKey:@"address"];
    
    [HttpTool postUrlWithString:url parameters:dic success:^(id responseObject) {
        
        NSError *error = [HttpTool inspectError:responseObject];
        if (error == nil) {
            
            success();
        }else{
            
            failure(error);
        }
        
    } failure:^(NSError *error) {
       
        failure(error);
    }];
}

#pragma mark - 删除地址
- (void)deleteAddressWithId:(NSString *)addressId Success:(void(^)(void))success Failure:(void(^)(NSError *error))failure{
    
    NSString *url = [NSString stringWithFormat:@"%@/api/address/deleltAddress",IP];
    NSDictionary *dic = @{
                          @"addressId":addressId
                          };
    
    [HttpTool postUrlWithString:url parameters:dic success:^(id responseObject) {
        
        NSError *error = [HttpTool inspectError:responseObject];
        if (error == nil) {

            
            success();
        }else{
            
            failure(error);
        }
        
    } failure:^(NSError *error) {
        
        failure(error);
    }];
}


#pragma mark - 编辑地址
- (void)editAdresstWithParameters:(AddAddressParametersModel *)parametersModel WithSuccess:(void (^)(void))success failure:(void (^)(NSError * error))failure{
    
    NSString *url = [NSString stringWithFormat:@"%@/api/address/updateAddress",IP];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:parametersModel.uid forKey:@"id"];
    [dic setObject:parametersModel.name forKey:@"name"];
    [dic setObject:parametersModel.phone forKey:@"phone"];
    [dic setObject:parametersModel.region forKey:@"region"];
    [dic setObject:parametersModel.address forKey:@"address"];
    
    [HttpTool postUrlWithString:url parameters:dic success:^(id responseObject) {
        
        NSError *error = [HttpTool inspectError:responseObject];
        if (error == nil) {
            
            success();
        }else{
            
            failure(error);
        }
        
    } failure:^(NSError *error) {
        
        failure(error);
    }];
}

#pragma mark - 设置默认地址
- (void)setDefaultAdresstWithAddressId:(NSString *)addressId andCustomId:(NSString *)customId Success:(void (^)(void))success failure:(void (^)(NSError * error))failure{
    
    NSString *url = [NSString stringWithFormat:@"%@/api/address/defaultaddress",IP];
    NSDictionary *dic = @{
                          @"customId":customId,
                          @"addressId":addressId
                          };
    
    [HttpTool postUrlWithString:url parameters:dic success:^(id responseObject) {
        
        NSError *error = [HttpTool inspectError:responseObject];
        if (error == nil) {
            
            success();
        }else{
            
            failure(error);
        }
        
    } failure:^(NSError *error) {
        
        failure(error);
    }];
}
@end
